package com.my.design.structural.proxy.cglibProxy;

import net.sf.cglib.proxy.*;

import java.lang.reflect.*;

/**
 * @author 代廷波
 * @className: CglibProxy
 * @description:
 * @create 2018/12/21 0021-14:49
 **/
public class CglibProxy implements MethodInterceptor {
private Enhancer enhancer = new Enhancer();


    public Object getProxy(Class clazz){
        // 设置创建子类的类
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }
    /**
     * @author 代廷波
     * @description:
     * @param  obj 目标类的实例
     * @param: method 目标方法的反射对象
     * @param: args 方法的参数
     * @param: methodProxy 代理类的实例
     * @return java.lang.Object
     * @create 2018-12-21 14:50
     **/
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        System.out.println("Cglib准备启动"+System.currentTimeMillis());
        methodProxy.invokeSuper(obj,args);
        System.out.println("Cglib准备停止"+System.currentTimeMillis());
        return null;
    }
}

